package com.hctg.forced2sleep.easy_window;

import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.actor.myandroidframework.utils.ConfigUtils;
import com.actor.myandroidframework.utils.LogUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.databinding.EasyWindowRectMenuBinding;
import com.hjq.window.EasyWindow;

/**
 * description: 元素选择 -> 3个菜单
 * company    :
 *
 * @author : ldf
 * date       : 2024/10/26 on 17
 * @version 1.0
 */
public class RectMenuEasyWindow extends EasyWindow2<RectMenuEasyWindow> implements EasyWindow.OnClickListener<View> {

    private final EasyWindowRectMenuBinding viewBinding;
    private final int                    dp12 = SizeUtils.dp2px(12);
    private Rect rectRoot, rect;

    public RectMenuEasyWindow(Rect rectRoot, Rect rect, boolean hasStatusBar) {
        super(ConfigUtils.APPLICATION);
        this.rectRoot = rectRoot;
        this.rect = rect;
        viewBinding = EasyWindowRectMenuBinding.inflate(LayoutInflater.from(getContext()), (ViewGroup) getDecorView(), false);
        setContentView(viewBinding.getRoot());
        viewBinding.btnOk.post(new Runnable() {
            @Override
            public void run() {
                updatePosition(rectRoot, rect, hasStatusBar);
            }
        });

        setGravity(Gravity.START | Gravity.TOP);
//        setXOffset(x);
//        setYOffset(y);
        setTag(this.getClass().getName());
        //添加监督
        setOnClickListener(viewBinding.btnOk.getId(), this);
        //取消
        setOnClickListener(viewBinding.btnMore.getId(), this);
        setOnClickListener(viewBinding.btn3.getId(), this);

//        showAsDropDown();
    }

    private OnItemClickListener listener;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public void onClick(EasyWindow<?> easyWindow, View view) {
        switch (view.getId()) {
            case R.id.btn_ok:   //标记元素
                if (listener != null) {
                    listener.onItemClick(rectRoot, rect, 0);
                }
                break;
            case R.id.btn_3:    //忽略元素
                if (listener != null) {
                    listener.onItemClick(rectRoot, rect, 1);
                }
                break;
            default:
                break;
        }
    }

    //更新显示位置
    public void updatePosition(Rect rectRoot, Rect rect, boolean hasStatusBar) {
        this.rectRoot = rectRoot;
        this.rect = rect;
        //Rect(0, 0 - 720, 1440)
        LogUtils.errorFormat("rectRoot: %s", rectRoot);
        //Rect(8, 792 - 148, 976)
        LogUtils.errorFormat("rect: %s", rect);

        //不能使用这个, ∵更新View后是垂直状态, root的宽度就是btn的宽度了.
//        int rootWidth = viewBinding.getRoot().getWidth();
        int btnWidth = viewBinding.btnOk.getWidth();
        int rootWidth = btnWidth * 3 + dp12;
        int btnHeight = viewBinding.btnOk.getHeight();
        LogUtils.errorFormat("rootWidth: %d, btnWidth: %d, btnHeight: %d", rootWidth, btnWidth, btnHeight);
        int xOffset = 0, yOffset = 0;

        //if选择的元素比本Window宽, 就水平排列(默认)
        if (rect.width() >= rootWidth) {
            viewBinding.getRoot().setOrientation(LinearLayout.HORIZONTAL);
            xOffset = rect.left + (rect.width() - rootWidth) / 2;
        } else if (rect.width() > btnWidth) {
            //if选择的元素比btn宽, 就垂直排列
            viewBinding.getRoot().setOrientation(LinearLayout.VERTICAL);
            xOffset = rect.left + (rect.width() - btnWidth) / 2;
        } else {
            //if选择的元素没有btn宽, 就垂直排列
            viewBinding.getRoot().setOrientation(LinearLayout.VERTICAL);
            xOffset = rect.left + (rect.width() - btnWidth) / 2;
            /**
             * 超出屏幕会自动停靠在侧边, 其实不用计算...
             */
            //if x 移出了左侧屏幕
            if (xOffset < rectRoot.left) {
                xOffset = rectRoot.left;
            } else if ((xOffset + btnWidth) > rectRoot.right) {
                //if x 移出了右侧屏幕
                xOffset = rectRoot.right - btnWidth;
            }
        }

        //if选择的元素比本Window宽, 就水平排列(默认)
        if (rect.width() >= rootWidth) {
            //if本Window放在Rect下方, 不会超出父界面
            if (rect.bottom + btnHeight <= rectRoot.bottom) {
                yOffset = rect.bottom;
            } else if (rect.top - btnHeight >= rectRoot.top) {
                //if放在rect顶部不超限
                yOffset = rect.top - btnHeight;
            } else {//否则就和rect底部对齐
                yOffset = rect.bottom - btnHeight;
            }
        } else {
            if (rect.bottom + btnHeight * 3 <= rectRoot.bottom) {
                yOffset = rect.bottom;
            } else if (rect.top - btnHeight * 3 >= rectRoot.top) {
                yOffset = rect.top - btnHeight * 3 - dp12;
            } else {
                yOffset = rect.bottom - btnHeight * 3 - dp12;
            }
        }

        if (hasStatusBar) {
            yOffset = yOffset - ConfigUtils.STATUS_BAR_HEIGHT;
        }
        setXOffset(xOffset);
        setYOffset(yOffset);
    }

    public interface OnItemClickListener {
        void onItemClick(Rect rectRoot, Rect rect, int position);
    }
}
